﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace FourPartHarmony3 {
    class RealizationSerializer {
        /// <summary>
        /// 保存 
        /// </summary>
        public static bool TrySave2(Realization realization, string path) {
            bool result = false;
            try {
                var musicSave = new MusicSave(realization);
                var s = new XmlSerializer(typeof(MusicSave));
                using (TextWriter w = new StreamWriter(File.Open(path, FileMode.Create))) {
                    s.Serialize(w, musicSave);
                }
                result = true;
            } catch (System.Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 読み出し 
        /// </summary>
        public static Realization TryLoad2(string path) {
            Realization result = null;
            try {
                var s = new XmlSerializer(typeof(MusicSave));
                using (TextReader r = new StreamReader(File.Open(path, FileMode.Open))) {
                    MusicSave ms = (MusicSave)s.Deserialize(r);

                    if (null != ms) {
                        result = ms.ToMusic();
                    }
                }
            } catch (System.Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            return result;
        }
    }
}
